﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Qetza.TeamFoundation.Build.Workflow.Activities;


namespace Qetza.TeamFoundation.Build.Workflow
{
    [TestClass]
    public class UpdateAssemblyInfoTest
    {
        #region Properties

        public TestContext TestContext { get; set; }

        #endregion

        #region Setup / Cleanup

        [TestCleanup]
        public void TestCleanup()
        {
            if (File.Exists("GlobalAssemblyInfo.cs"))
            {
                File.Delete("GlobalAssemblyInfo.cs");
            }

            if (File.Exists("GlobalAssemblyInfo2.cs"))
            {
                File.Delete("GlobalAssemblyInfo2.cs");
            }
        }

        #endregion

        #region Tests

        [TestMethod]
        public void ExecuteWithOneFileTest()
        {
            // arrange
            File.Copy("TestAssemblyInfo.cs", "GlobalAssemblyInfo.cs", true);

            var activity = new UpdateAssemblyInfo();
            var parameters = new Dictionary<string, object>() {
                { "AssemblyInfoFiles", new [] { "GlobalAssemblyInfo.cs" } },
                { "AssemblyVersionFormat", "$(current).$(current).0.0" },
                { "AssemblyFileVersionFormat", "$(current).$(current).$(increment).0" },
                { "AssemblyInformationalVersionFormat", "Test1 $(version) $(fileversion) $(date:yyMMdd)" },
                };

            // act
            var actual = WorkflowInvoker.Invoke(activity, parameters);

            // assert
            Assert.AreEqual("2.1.0.0", actual["MaxAssemblyVersion"].ToString());
            Assert.AreEqual("3.4.1.0", actual["MaxAssemblyFileVersion"].ToString());
            Assert.AreEqual(
                "Test1 2.1.0.0 3.4.1.0 " + DateTime.Now.ToString("yyMMdd", System.Globalization.CultureInfo.InvariantCulture), 
                actual["MaxAssemblyInformationalVersion"].ToString()
                );
        }

        [TestMethod]
        public void ExecuteWithTwoFileTest()
        {
            // arrange
            File.Copy("TestAssemblyInfo.cs", "GlobalAssemblyInfo.cs", true);
            File.Copy("TestAssemblyInfo2.cs", "GlobalAssemblyInfo2.cs", true);

            var activity = new UpdateAssemblyInfo();
            var parameters = new Dictionary<string, object>() {
                { "AssemblyInfoFiles", new [] { "GlobalAssemblyInfo.cs", "GlobalAssemblyInfo2.cs" } },
                { "AssemblyVersionFormat", "$(current).$(current).0.0" },
                { "AssemblyFileVersionFormat", "$(current).$(current).$(increment).0" },
                { "AssemblyInformationalVersionFormat", "Test2 $(version) $(fileversion) $(date:yyMMdd)" },
                };

            // act
            var actual = WorkflowInvoker.Invoke(activity, parameters);

            // assert
            Assert.AreEqual("2.3.0.0", actual["MaxAssemblyVersion"].ToString());
            Assert.AreEqual("3.4.1.0", actual["MaxAssemblyFileVersion"].ToString());
            Assert.AreEqual(
                "Test2 2.3.0.0 2.3.5.0 " + DateTime.Now.ToString("yyMMdd", System.Globalization.CultureInfo.InvariantCulture),
                actual["MaxAssemblyInformationalVersion"].ToString()
                );
        }

        #endregion
    }
}
